/* Copyright 2008 Bas van den Berg
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <string>

#include "yaffut.h"
#include "myassert.h"

#include "TaskQueue.h"
#include "Global.h"
#include "MudMobile.h"

using namespace std;
using namespace Chronos;


class TestMobile : public MudMobile {
public:
	TestMobile(const std::string& name_) : MudMobile(name_, "descr", 100, 2, 10, 50) {}

    virtual unsigned int doCombat() { return 0; }
    virtual void miss(MudMobile&) {}
    virtual bool takeDmg(MudMobile&, unsigned int) { return false; }
};


struct MudMobileTest : public MudMobile {
    MudMobileTest()
		: MudMobile("mobile", "descr", 100, 2, 10, 50)
		, taskqueue(0)
		, combatCalled(false)
		, corpseCreated(false)
    {
        Global<TaskQueue>::set(&taskqueue);
    }
    virtual unsigned int doCombat() { combatCalled = true; return stats[Stats::speed]; }
    virtual void miss(MudMobile&) {}
    virtual bool takeDmg(MudMobile&, unsigned int) { return false; }
	virtual void createCorpse(const std::string&) { corpseCreated = true; }

    TaskQueue taskqueue;
	bool combatCalled;
	bool corpseCreated;
};


TEST(MudMobileTest, testDestruction) {
	{
  		TestMobile mobile2("mobile2");
		addTarget(mobile2);
		mobile2.addTarget(*this);
		ASSERT_TRUE(isInCombat());
		ASSERT_TRUE(combatTarget == &mobile2);
	}
	ASSERT_FALSE(isInCombat());
	ASSERT_NULL(combatTarget);
}


TEST(MudMobileTest, testAddingTarget) {
  	TestMobile mobile2("mobile2"); 
	addTarget(mobile2);
	ASSERT_TRUE(isInCombat());
	ASSERT_FALSE(mobile2.isInCombat());
	ASSERT_TRUE(combatTarget == &mobile2);
	ASSERT_FALSE(combatCalled);

	taskqueue.runNextTime();
	ASSERT_TRUE(combatCalled);

  	TestMobile mobile3("mobile3"); 
	addTarget(mobile3);
	ASSERT_TRUE(combatTarget == &mobile2);
}


TEST(MudMobileTest, testDie) {
  	TestMobile mobile2("mobile2"); 
	addTarget(mobile2);
	die("cause");
	ASSERT_EQUAL(0, getHealth());
	ASSERT_FALSE(isInCombat());
	ASSERT_NULL(combatTarget);
	ASSERT_TRUE(corpseCreated);
}

